package com.atguigu.tingshu.album.mapper;

import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TrackInfoMapper extends BaseMapper<TrackInfo> {

    /**
     * 查询当前用户的声音分页列表
     * @param pageInfo 分页信息
     * @param trackInfoQuery 声音查询条件 标题 状态 (是否审核通过?) 用户id(当前登录的id)
     * @return page 分页
     */
    Page<TrackListVo> getUserTrackByPage(Page<TrackListVo> pageInfo, @Param("vo") TrackInfoQuery trackInfoQuery);

    @Update("update track_info set order_num = order_num -1 where album_id = #{albumId} and order_num > #{orderNum} and is_deleted = 0")
    void updateTrackNum(@Param("albumId") Long albumId, @Param("orderNum") Integer orderNum);

    Page<AlbumTrackListVo> getUserAlbumTrackPage(@Param("vo") Page<AlbumTrackListVo> pageInfo, @Param("albumId") Long albumId);

    /**
     * 根据声音id,统计当前声音的点赞 评论 右下角的爱心 评论数量
     * @param trackId 声音id
     * @return 统计信息
     * */

    @Select("select\n" +
            "    track_id,\n" +
            "    max(if(stat_type='0701', stat_num, 0)) playStatNum,\n" +
            "    max(if(stat_type='0702', stat_num, 0)) collectStatNum,\n" +
            "    max(if(stat_type='0703', stat_num, 0)) praiseStatNum,\n" +
            "    max(if(stat_type='0704', stat_num, 0)) commentStatNum\n" +
            "    from track_stat where track_id = #{trackId} and is_deleted=0\n" +
            "group by track_id")
    TrackStatVo getTrackStatVo(@Param("trackId") Long trackId);
}
